package com.example.sunlight.interneteverything;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.sunlight.interneteverything.Adapter.ShopBase;


import java.util.ArrayList;
import java.util.List;

import data.Datas;
import server.MyAlerDialog;

/**
 * Created by SunLight on 2017/8/18.
 */

public class IntelligentStorage extends Activity implements View.OnClickListener {
    ImageView wz;
    ListView listView;
    ShopBase shopBase;
    List<String[]> shop;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x123:
                    System.out.println("cqwonvrulz   " + text);
                    switch (text) {
                        case Datas.ONE:
                            shop.set(0, new String[]{"02799", "水果类", "蛇果", "05", "01", --one + "", "正常"});

                            break;
                        case Datas.TWO:
                            shop.set(1, new String[]{"05478", "水果类", "杨桃", "08", "07", --two + "", "正常"});
                            break;
                        case Datas.THREE:
                            shop.set(2, new String[]{"02733", "水果类", "柠檬", "08", "07", --three + "", "正常"});
                            break;
                        case Datas.FOUR:
                            shop.set(3, new String[]{"02756", "水果类", "香蕉", "05", "01", --four + "", "正常"});
                            break;
                        case Datas.FIVE:
                            shop.set(4, new String[]{"05873", "水果类", "桔子", "08", "07", --five + "", "正常"});
                            break;
                        case Datas.SIX:
                            shop.set(5, new String[]{"02758", "水果类", "桃子", "05", "01", --six + "", "正常"});
                            break;
                        case Datas.SEVEN:
                            shop.set(6, new String[]{"02746", "水果类", "梨子", "05", "01", --seven + "", "正常"});
                            break;
                        case Datas.EIGHT:
                            shop.set(7, new String[]{"02711", "水果类", "石榴", "05", "01", --eight + "", "正常"});
                            break;
                        case Datas.NINE:
                            shop.set(8, new String[]{"02789", "水果类", "芒果", "05", "01", --nine + "", "正常"});
                            break;
                        case Datas.TEN:
                            shop.set(9, new String[]{"02744", "水果类", "草莓", "05", "01", --ten + "", "正常"});
                            break;
                        case Datas.ELEVEN:
                            shop.set(10, new String[]{"02784", "办公用品", "晨光铅笔", "08", "02", --eleven + "", "正常"});
                            break;
                        case Datas.TLEVEN:
                            shop.set(11, new String[]{"01235", "水果类", "红苹果", "08", "05", --tleven + "", "正常"});
                            break;
                        case Datas.THREHC:
                            shop.set(12, new String[]{"02784", "水果类", "青苹果", "08", "04", --threhc + "", "正常"});
                            break;
                        case Datas.PCIKAS:
                            shop.set(13, new String[]{"02456", "办公用品", "南孚电池", "03", "01", --pcikas + "", "正常"});
                            break;
                        case Datas.WITCH:
                            shop.set(14, new String[]{"02168", "办公用品", "晨光白板笔", "08", "09", --witch + "", "正常"});
                            break;
                    }
                    shopBase.setData(shop);
                    listView.setAdapter(shopBase);
                    shopBase.notifyDataSetChanged();
                    break;
            }
        }
    };

    int one = 300, two = 216, three = 213, four = 256, five = 213, six = 213, seven = 260, eight = 213, nine = 213, ten = 213, eleven = 210,
            tleven = 180, threhc = 100, pcikas = 55, witch = 98;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_storage);
        wz = (ImageView) findViewById(R.id.wz);
        listView = (ListView) findViewById(R.id.list);
        shopBase = new ShopBase(this);
        shop = new ArrayList<>();
        wz.setOnClickListener(this);
        addList("02799", "水果", "蛇果", "05", "01", one + "", "正常");
        addList("05478", "水果", "杨桃", "08", "07", two + "", "正常");
        addList("02733", "水果", "柠檬", "08", "07", three + "", "正常");
        addList("02756", "水果", "香蕉", "05", "01", four + "", "正常");
        addList("05873", "水果", "桔子", "08", "07", five + "", "正常");
        addList("02758", "水果", "桃子", "05", "01", six + "", "正常");
        addList("02746", "水果", "梨子", "05", "01", seven + "", "正常");
        addList("02711", "水果", "石榴", "05", "01", eight + "", "正常");
        addList("02789", "水果", "芒果", "05", "01", nine + "", "正常");
        addList("02744", "水果", "草莓", "05", "01", ten + "", "正常");
        addList("02784", "办公用品", "晨光铅笔", "08", "02", eleven + "", "正常");
        addList("01235", "水果", "红苹果", "08", "05", tleven + "", "正常");
        addList("02784", "水果", "青苹果", "08", "04", threhc + "", "正常");
        addList("02456", "办公用品", "南孚电池", "03", "01", pcikas + "", "正常");
        addList("02168", "办公用品", "晨光白板笔", "08", "09", witch + "", "正常");
        myReceiver = new MyReceiver();
        IntentFilter filter = new IntentFilter("com.brock.rfid");
        registerReceiver(myReceiver, filter);
        shopBase.setData(shop);
        listView.setAdapter(shopBase);
        shopBase.notifyDataSetChanged();
    }

    MyReceiver myReceiver;

    public void addList(String id, String type, String name, String storage, String shelf, String number, String state) {
        if (Integer.parseInt(number) < 3) {
            state = "库存紧张";
        }
        shop.add(new String[]{id, type, name, storage, shelf, number, state});
    }


    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.wz) {
            shopBase.setData(shop);
            listView.setAdapter(shopBase);
            shopBase.notifyDataSetChanged();
        }
    }

    String text = "";

    class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("com.brock.rfid")) {
                text = intent.getStringExtra("text");
                handler.sendEmptyMessage(0x123);
            }
        }
    }
}
